function [image_score_struct] = image_check_result(ground_truth,image_labels, report_flag)

% This function is calculating the success and a failure of Image Labels function 
% compared to the Ground Truth

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% ground_truth - labels matrix, output of GUI decipher tool.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% image_labels - labels matrix, output of Image Labels function.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% report_flag  - '1' if we want to print on screen a report, '0' Otherwise.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% image_score_struct - struct with these fields:
%                                       image_score_arr(5,5) - 
%                                                                        Each row - label by ground truth.
%                                                                        Each column - label by image labels.
%                                       tru_pct_1 - tru_pct_4 : a percent of correct labelings for each label.
%                                       tru_total_pct : total percent of correct labels.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ground_mask_0 = (ground_truth==0);
ground_mask_1 = (ground_truth==1);
ground_mask_2 = (ground_truth==2);
ground_mask_3 = (ground_truth==3);
ground_mask_4 = (ground_truth==4);
%im_size = size(ground_truth,1)*size(ground_truth,2);

image_labels_mask_0 = (image_labels==0);
image_labels_mask_1 = (image_labels==1);
image_labels_mask_2 = (image_labels==2);
image_labels_mask_3 = (image_labels==3);
image_labels_mask_4 = (image_labels==4);

image_score_struct.image_score_arr = zeros(5,5);
image_score_struct.image_score_arr(1,1) = sum(sum(ground_mask_1 .* image_labels_mask_1));
image_score_struct.image_score_arr(1,2) = sum(sum(ground_mask_1 .* image_labels_mask_2));
image_score_struct.image_score_arr(1,3) = sum(sum(ground_mask_1 .* image_labels_mask_3));
image_score_struct.image_score_arr(1,4) = sum(sum(ground_mask_1 .* image_labels_mask_4));
image_score_struct.image_score_arr(1,5) = sum(sum(ground_mask_1 .* image_labels_mask_0));

image_score_struct.image_score_arr(2,1) = sum(sum(ground_mask_2 .* image_labels_mask_1));
image_score_struct.image_score_arr(2,2) = sum(sum(ground_mask_2 .* image_labels_mask_2));
image_score_struct.image_score_arr(2,3) = sum(sum(ground_mask_2 .* image_labels_mask_3));
image_score_struct.image_score_arr(2,4) = sum(sum(ground_mask_2 .* image_labels_mask_4));
image_score_struct.image_score_arr(2,5) = sum(sum(ground_mask_2 .* image_labels_mask_0));

image_score_struct.image_score_arr(3,1) = sum(sum(ground_mask_3 .* image_labels_mask_1));
image_score_struct.image_score_arr(3,2) = sum(sum(ground_mask_3 .* image_labels_mask_2));
image_score_struct.image_score_arr(3,3) = sum(sum(ground_mask_3 .* image_labels_mask_3));
image_score_struct.image_score_arr(3,4) = sum(sum(ground_mask_3 .* image_labels_mask_4));
image_score_struct.image_score_arr(3,5) = sum(sum(ground_mask_3 .* image_labels_mask_0));

image_score_struct.image_score_arr(4,1) = sum(sum(ground_mask_4 .* image_labels_mask_1));
image_score_struct.image_score_arr(4,2) = sum(sum(ground_mask_4 .* image_labels_mask_2));
image_score_struct.image_score_arr(4,3) = sum(sum(ground_mask_4 .* image_labels_mask_3));
image_score_struct.image_score_arr(4,4) = sum(sum(ground_mask_4 .* image_labels_mask_4));
image_score_struct.image_score_arr(4,5) = sum(sum(ground_mask_4 .* image_labels_mask_0));

image_score_struct.image_score_arr(5,1) = sum(sum(ground_mask_0 .* image_labels_mask_1));
image_score_struct.image_score_arr(5,2) = sum(sum(ground_mask_0 .* image_labels_mask_2));
image_score_struct.image_score_arr(5,3) = sum(sum(ground_mask_0 .* image_labels_mask_3));
image_score_struct.image_score_arr(5,4) = sum(sum(ground_mask_0 .* image_labels_mask_4));
image_score_struct.image_score_arr(5,5) = sum(sum(ground_mask_0 .* image_labels_mask_0));

image_score_struct.total_1 = sum(image_score_struct.image_score_arr(1,:));
image_score_struct.total_2 = sum(image_score_struct.image_score_arr(2,:));
image_score_struct.total_3 = sum(image_score_struct.image_score_arr(3,:));
image_score_struct.total_4 = sum(image_score_struct.image_score_arr(4,:));

image_score_struct.total_vote_1 = sum(image_score_struct.image_score_arr(:,1));
image_score_struct.total_vote_2 = sum(image_score_struct.image_score_arr(:,2));
image_score_struct.total_vote_3 = sum(image_score_struct.image_score_arr(:,3));
image_score_struct.total_vote_4 = sum(image_score_struct.image_score_arr(:,4));

if (image_score_struct.total_1>0)
    image_score_struct.tru_pct_1 = image_score_struct.image_score_arr(1,1)/image_score_struct.total_1;
    image_score_struct.vote_tru_pct_1 = image_score_struct.image_score_arr(1,1)/image_score_struct.total_vote_1;
else
    image_score_struct.tru_pct_1 = 0;
    image_score_struct.vote_tru_pct_1 = 0;
end
if (image_score_struct.total_2>0)
    image_score_struct.tru_pct_2 = image_score_struct.image_score_arr(2,2)/image_score_struct.total_2;
    image_score_struct.vote_tru_pct_2 = image_score_struct.image_score_arr(2,2)/image_score_struct.total_vote_2;
else
    image_score_struct.tru_pct_2 = 0;
    image_score_struct.vote_tru_pct_2 = 0;
end
if (image_score_struct.total_3>0)
    image_score_struct.tru_pct_3 = image_score_struct.image_score_arr(3,3)/image_score_struct.total_3;
    image_score_struct.vote_tru_pct_3 = image_score_struct.image_score_arr(3,3)/image_score_struct.total_vote_3;
else
    image_score_struct.tru_pct_3 = 0;    
    image_score_struct.vote_tru_pct_3 = 0;
end
if (image_score_struct.total_4>0)
    image_score_struct.tru_pct_4 = image_score_struct.image_score_arr(4,4)/image_score_struct.total_4;
    image_score_struct.vote_tru_pct_4 = image_score_struct.image_score_arr(4,4)/image_score_struct.total_vote_4;
else
    image_score_struct.tru_pct_4 = 0;    
    image_score_struct.vote_tru_pct_4 = 0;
end

image_score_struct.tru_total_pct = (image_score_struct.image_score_arr(1,1) + image_score_struct.image_score_arr(2,2) + image_score_struct.image_score_arr(3,3) + image_score_struct.image_score_arr(4,4))/(image_score_struct.total_1 + image_score_struct.total_2 + image_score_struct.total_3 + image_score_struct.total_4);
image_score_struct.tru_total_vote_pct  =  (image_score_struct.image_score_arr(1,1) + image_score_struct.image_score_arr(2,2) + image_score_struct.image_score_arr(3,3) + image_score_struct.image_score_arr(4,4))/(image_score_struct.total_vote_1 + image_score_struct.total_vote_2 + image_score_struct.total_vote_3 + image_score_struct.total_vote_4);

if (report_flag==1)
    fprintf('************************************************************************************************************** \n');
    fprintf('Label 1 from ground truth success percent: ');fprintf(num2str(image_score_struct.tru_pct_1));fprintf('\n');
    fprintf('Label 2 from ground truth success percent: ');fprintf(num2str(image_score_struct.tru_pct_2));fprintf('\n');
    fprintf('Label 3 from ground truth success percent: ');fprintf(num2str(image_score_struct.tru_pct_3));fprintf('\n');
    fprintf('Label 4 from ground truth success percent: ');fprintf(num2str(image_score_struct.tru_pct_4));fprintf('\n');
    fprintf('Total success percent from ground truth : ');fprintf(num2str(image_score_struct.tru_total_pct));fprintf('\n');    
     fprintf('Total fail percent from ground truth : ');fprintf(num2str(1-image_score_struct.tru_total_pct));fprintf('\n');    
    fprintf('************************************************************************************************************** \n');
    fprintf('Label 1 from image labeling success percent: ');fprintf(num2str(image_score_struct.vote_tru_pct_1));fprintf('\n');
    fprintf('Label 2 from image labeling success percent: ');fprintf(num2str(image_score_struct.vote_tru_pct_2));fprintf('\n');
    fprintf('Label 3 from image labeling success percent: ');fprintf(num2str(image_score_struct.vote_tru_pct_3));fprintf('\n');
    fprintf('Label 4 from image labeling success percent: ');fprintf(num2str(image_score_struct.vote_tru_pct_4));fprintf('\n');
    fprintf('Total success percent from image labeling : ');fprintf(num2str(image_score_struct.tru_total_vote_pct));fprintf('\n');    
    fprintf('Total fail percent from image labeling : ');fprintf(num2str(1-image_score_struct.tru_total_vote_pct));fprintf('\n');    
    fprintf('************************************************************************************************************** \n');
end